package com.retoc.workflowable.controller;

import com.retoc.workflowable.common.R;
import com.retoc.workflowable.pojo.CarReq;
import com.retoc.workflowable.pojo.EvectionReq;
import org.flowable.engine.*;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/evection")
public class EvectionController {
    @Autowired
    ProcessEngine processEngine;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    HistoryService historyService;

    @PostMapping("/evectionRequest")
    public R evectionRequest(@Valid @RequestBody EvectionReq req){
        Map<String,Object> map=new HashMap<>();
        map.put("url", req.getUrl());
        map.put("createrId", req.getCreaterId());
        map.put("managerId", req.getManagerId());
        map.put("gmId", req.getGmId());
        map.put("needGm", req.getNeedGm());
        map.put("needCar", req.getNeedCar());
        map.put("dates", req.getDates());
        map.put("types", req.getTypes());
        map.put("uuid", req.getUuid());

        ProcessInstance evectionReq = runtimeService.startProcessInstanceByKey("evectionReq", map);

        Task task = taskService.createTaskQuery()
                .processInstanceId(evectionReq.getId())
                .taskAssignee(req.getCreaterId()+"")
                .singleResult();
        taskService.complete(task.getId());

        Map<String,Object> result=new HashMap<>();

        //如果需要申请用车，顺便创建用车申请工作流并把工作流id写入结果返回
        if (req.getNeedCar()!=0){
            Map<String,Object> car=new HashMap<>();
            car.put("url", req.getUrl());
            car.put("createrId", req.getCreaterId());
            car.put("managerId", req.getManagerId());
            car.put("adId", req.getAdId());
            car.put("dates", req.getDates());
            car.put("driver", req.getDriver());
            car.put("types","car");
            car.put("uuid", req.getUuid());

            ProcessInstance carReq = runtimeService.startProcessInstanceByKey("carReq", car);

            Task carTask = taskService.createTaskQuery()
                    .processInstanceId(carReq.getId())
                    .taskAssignee(req.getCreaterId()+"")
                    .singleResult();
            taskService.complete(carTask.getId());
            result.put("carInstanceId",carReq.getId());
        }

        result.put("instanceId",evectionReq.getId());
        result.put("types", req.getTypes());
        result.put("uuid", req.getUuid());
        return R.ok().put("result",result);
    }

}
